#include "sensor_config.h"
#include "tftp_server.h"
#include "eeprom.h"

extern uint32_t task_flag;
/*lis2dh12*********************************************************************************************/
bool _parameter_cmd_open(char* name, uint8_t mode)
{
    if (mode != TFTP_WRQ) return false;
    
    if (0 == strcmp(name, "parameter.write")) {
        return true;
    } else {
        return false;
    }
}

uint16_t (_parameter_cmd_read)(uint8_t blk, uint8_t* buffer)
{
    return 0;
}

static void record_sample_period_in_eeprom(uint16_t period)
{
    eeprom_data_t eeprom_data;
    uint32_t sample_period = 0;
 
    sample_period = period;
 
    eeprom_erase(1, EEPROM_DATA_3AXIS_SAMPLE_PERIOD_VALUE_ADD);
   
    eeprom_data.len = 1;
    eeprom_data.address = EEPROM_DATA_3AXIS_SAMPLE_PERIOD_VALUE_ADD;
    eeprom_data.eeprom_data = &sample_period;
 
    eeprom_write(eeprom_data);

}

void (_parameter_cmd_write)(uint8_t blk, uint8_t* buffer, uint16_t len)
{
     uint16_t word_len = 0;
     eeprom_data_t eeprom_data;
     uint32_t *p = (uint32_t*)buffer;
     
     if(len != EEPORM_PARAMETER_SIZE)
          app_fault_handle(APP_LIS2DH12_CONFIG_ERROR);
     
     
     {
         lis2dh12_param_t* param_data = (void*)buffer;
         record_sample_period_in_eeprom(param_data->sample_period);
     }
     
     word_len = (EEPORM_PARAMETER_SIZE + 3) / 4;
     eeprom_erase(word_len, EEPROM_PARAMETER_ADDR);
     
     eeprom_data.len = word_len;
     eeprom_data.address = EEPROM_PARAMETER_ADDR;
     eeprom_data.eeprom_data = p;
     
     eeprom_write(eeprom_data);
 
}

void (_parameter_cmd_close)(void)
{
    task_flag = TRUE;
}

const tftp_server_plugin_t tftp_server_plugin_parameter_cmd = {
    _parameter_cmd_open,
    _parameter_cmd_read,
    _parameter_cmd_write,
    _parameter_cmd_close,
};


